package mystack;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2024-07-18
 * Time: 20:38
 */
public class MyStack {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_CAPACITY = 10;

    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }

    public MyStack(int capacity){
        this.elem = new int[capacity];
    }

    private boolean checkCapacity(){
        if(this.usedSize == elem.length){
            return true;
        }
        return false;
    }

    public void push(int val){
        if(this.checkCapacity()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;
    }

    public int pop() throws EmptyException {
        if(this.usedSize == 0){
            //throw new EmptyException("栈为空！");
            return -1;
        }
        int tmp = this.elem[this.usedSize-1];
        this.usedSize--;
        return tmp;
    }

    public int peek() throws EmptyException {
        if(this.usedSize == 0){
            //throw new EmptyException("栈为空！");
            return -1;
        }
        int tmp = this.elem[this.usedSize-1];
        return tmp;
    }

    public int size(){
        return this.usedSize;
    }

    public boolean empty(){
        return this.usedSize == 0;
    }
}
